package com.ktz.shoping.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktz.shoping.bean.UserBean;
import com.ktz.shoping.dao.UserDao;
import com.ktz.shoping.dao.UserDaoFactary;
import com.ktz.shoping.myutils.MyUtil;

@WebServlet("/SaveUserServlet")
@MultipartConfig(maxFileSize=1024*1024*5)
public class SaveUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public SaveUserServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.getSession().removeAttribute("modifyAdminB");
		UserBean user = MyUtil.convertRequestParameter2Bean(request, UserBean.class,"updatefiles/adminImgs/","admin");
		UserDao uDao = UserDaoFactary.getUserDaoInstance();
		int result = uDao.saveOrUpdateAdminInfo(user);
		if (result > 0) {
			// 查询获取要显示的数据集合
			List<UserBean> lstAdmins =  UserDaoFactary.getUserDaoInstance().getAllAdminInfo(0);
			// 将查询的结果集缓存到session中，在前端使用el表达式进行解析显示
			request.getSession().setAttribute("lstAdmins", lstAdmins);
			request.getSession().setAttribute("userinfo", "新增用户成功");
		} else {
			request.getSession().setAttribute("userinfo", "新增用户失败");
		}
		response.sendRedirect("admin/savesuccess.jsp");
	}

}
